﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RapidSearcher
{
    public partial class Configuration : Form
    {
        public Configuration()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SearchOrder.Text = "1080|720|bluray|hdrip|bdrip|brrip|dvdrip|hdtv|xvid|r5|cam|kam";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SearchOrder.Text = "bluray|hdrip|dvdrip|hdtv|xvid";
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.SearchOrder = SearchOrder.Text;

            if (UseRapidshare.Checked)
            {
                Properties.Settings.Default.RsCreds = RsUsername.Text + ":" + RsPassword.Text;
            }
            else
            {
                Properties.Settings.Default.RsCreds = "";
            }

            if (UseDestroot.Checked)
            {
                Properties.Settings.Default.DestRoot = DestRoot.Text;
            }
            else
            {
                Properties.Settings.Default.DestRoot = "";
            }

            Properties.Settings.Default.IDMPath =
                Properties.Settings.Default.FDMPath = 
                Properties.Settings.Default.JDPath = "";
            if (IDMOption.Checked)
                Properties.Settings.Default.IDMPath = DmPath.Text;
            else if (FDMOption.Checked)
                Properties.Settings.Default.FDMPath = DmPath.Text;
            else if (JDownloaderOption.Checked)
                Properties.Settings.Default.JDPath = DmPath.Text;

            Properties.Settings.Default.DlPath = DlPath.Text;
            
            Properties.Settings.Default.Save();
            Program.InitFromConfiguration();
            this.Hide();
        }

        private void Configuration_Load(object sender, EventArgs e)
        {
            SearchOrder.Text = Properties.Settings.Default.SearchOrder;
            if (!string.IsNullOrEmpty(Properties.Settings.Default.RsCreds))
            {
                UseRapidshare.Checked = true;
                RsUsername.Text = Properties.Settings.Default.RsCreds.Substring(0, Properties.Settings.Default.RsCreds.IndexOf(":"));
                RsPassword.Text = Properties.Settings.Default.RsCreds.Substring(Properties.Settings.Default.RsCreds.IndexOf(":") + 1);
            }
            else
            {
                UseRapidshare.Checked = false;
            }
            UseRapidshare_CheckedChanged(null, null);

            if (!string.IsNullOrEmpty(Properties.Settings.Default.DestRoot))
            {
                UseDestroot.Checked = true;
                DestRoot.Text = Properties.Settings.Default.DestRoot;
            }
            else
            {
                UseDestroot.Checked = false;
            }
            UseDestroot_CheckedChanged(null,null);

            if (!string.IsNullOrEmpty(Properties.Settings.Default.IDMPath))
            {
                IDMOption.Checked = true;
                DmPath.Text = Properties.Settings.Default.IDMPath;
            }
            else if (!string.IsNullOrEmpty(Properties.Settings.Default.FDMPath))
            {
                FDMOption.Checked = true;
                DmPath.Text = Properties.Settings.Default.FDMPath;
            }
            else if (!string.IsNullOrEmpty(Properties.Settings.Default.JDPath))
            {
                JDownloaderOption.Checked = true;
                DmPath.Text = Properties.Settings.Default.JDPath;
            }

            DlPath.Text = Properties.Settings.Default.DlPath;
        }

        private void UseRapidshare_CheckedChanged(object sender, EventArgs e)
        {
            useRsFrame.Enabled = UseRapidshare.Checked;
        }

        private void UseDestroot_CheckedChanged(object sender, EventArgs e)
        {
            useDestRootFrame.Enabled = UseDestroot.Checked;
        }

        private void browseDmPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.ShowNewFolderButton = false;
            dlg.Description = "Select download manager folder";
            DialogResult res = dlg.ShowDialog();
            if (res == System.Windows.Forms.DialogResult.OK)
            {
                DmPath.Text = dlg.SelectedPath;
            }
        }

        private void browseDestRootDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.ShowNewFolderButton = false;
            dlg.Description = "Select root folder for titles directories";
            DialogResult res = dlg.ShowDialog();
            if (res == System.Windows.Forms.DialogResult.OK)
            {
                DestRoot.Text = dlg.SelectedPath;
            }

        }

        private void browseDlPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.ShowNewFolderButton = false;
            dlg.Description = "Select downloads folder";
            DialogResult res = dlg.ShowDialog();
            if (res == System.Windows.Forms.DialogResult.OK)
            {
                DlPath.Text = dlg.SelectedPath;
            }

        }
    }
}
